<?php

/**
 * Convert column mapper
 *
 * @category   Mage
 * @package    Mage_Dataflow
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Xzsdsplus_Importplus_Model_Convert_Mapper_Column extends Mage_Dataflow_Model_Convert_Mapper_Column
{

    public function map() {
        
        //$attributes = Mage::getSingleton('catalog/convert_parser_product') ->getExternalAttributes();
        $status = Mage::getSingleton('importplus/status');
        $batchModel  = $this->getBatchModel();
        $batchExport = $this->getBatchExportModel();

        $batchExportIds = $batchExport
            ->setBatchId($this->getBatchModel()->getId())
            ->getIdCollection();

        $onlySpecified = (bool)$this->getVar('_only_specified') === true;

        if (!$onlySpecified) {
            foreach ($batchExportIds as $batchExportId) {
                $batchExport->load($batchExportId);
                $batchModel->parseFieldList($batchExport->getBatchData());
            }

            return $this;
        }

        if ($this->getVar('map') && is_array($this->getVar('map'))) {
            $attributesToSelect = $this->getVar('map');
        }
        else {
            $attributesToSelect = array();
        }

        if (!$attributesToSelect) {
            $this->getBatchExportModel()
                ->setBatchId($this->getBatchModel()->getId())
                ->deleteCollection();

            throw new Exception(Mage::helper('dataflow')->__('Error field mapping! Fields list for mapping is not defined'));
        }
        

        foreach ($batchExportIds as $batchExportId) {
            $batchExport = $this->getBatchExportModel()->load($batchExportId);
            $row = $batchExport->getBatchData();

            $newRow = array();
            
            if ($status->getCustomOptions()) {
                /*
                foreach($row as $key=>$val) {
                    if (!array_key_exists($key, $attributes)) {
                        $newRow[$key] = $row[$key];
                    }
                }*/
                foreach($row as $key=>$val) {
                    if (strpos ( $key, ':' ) !== FALSE && strlen ( $val )){
                        $newRow[$key] = $row[$key];
                    }
                }
            }
            
            if ($status->getExportReviews()) {
                $newRow['review'] = isset($row['review']) ? $row['review'] : null;
                $newRow['clearreview'] = 1;
            }
            
            if ($status->getCustomFields()) {
                foreach ($attributesToSelect as $field => $mapField) {
                    $newRow[$mapField] = isset($row[$mapField]) ? $row[$mapField] : null;
                }
            } else {
                foreach ($attributesToSelect as $field => $mapField) {
                    $newRow[$mapField] = isset($row[$field]) ? $row[$field] : null;
                }
            }
            
            $batchExport->setBatchData($newRow)
                ->setStatus(2)
                ->save();
            $this->getBatchModel()->parseFieldList($batchExport->getBatchData());
        }

        return $this;
    }
}